<?php

namespace app\admin\controller;

use app\common\controller\Admin;
use think\Request;

//首页控制器
class Index extends Admin
{
    public function setting()
    {
        //今天
        $today = date('Y-m-d');
        //用户模块
        //所有用户
        $alluser = count(model('user')->field('id')->select()->toArray());
        $allusertime = count(model('user')->field('id')->where('create_time','>=',$today)->select()->toArray());
        //普通会员
        $ptuser = count(model('user')->field('id')->where('ptsf','=','1')->select()->toArray());
        $ptusertime = count(model('user')->field('id')->where([
            ['ptsf','=','1'],
            ['create_time','>=','$today']
        ])->select()->toArray());
        //VIP会员
        $vipuser = count(model('user')->field('id')->where('ptsf','=','2')->select()->toArray());
        $vipusertime = count(model('user')->field('id')->where([
            ['ptsf','=','2'],
            ['create_time','>=','$today']
        ])->select()->toArray());
        //普通代理
        $pduser = count(model('user')->field('id')->where('ptsf','=','3')->select()->toArray());
        $pdusertime = count(model('user')->field('id')->where([
            ['ptsf','=','3'],
            ['create_time','>=','$today']
        ])->select()->toArray());
        //区产品代理
        $cpquser = count(model('user')->field('id')->where('canpinsf','=','4')->select()->toArray());
        $cpqusertime = count(model('user')->field('id')->where([
            ['canpinsf','=','4'],
            ['create_time','>=','$today']
        ])->select()->toArray());
        //市产品代理
        $cpsuser = count(model('user')->field('id')->where('canpinsf','=','5')->select()->toArray());
        $cpsusertime = count(model('user')->field('id')->where([
            ['canpinsf','=','5'],
            ['create_time','>=','$today']
        ])->select()->toArray());
        //省产品代理
        $cpshuser = count(model('user')->field('id')->where('canpinsf','=','7')->select()->toArray());
        $cpshusertime = count(model('user')->field('id')->where([
            ['canpinsf','=','7'],
            ['create_time','>=','$today']
        ])->select()->toArray());
        //合伙人
        $hhuser = count(model('user')->field('id')->where('hhren','=','1')->select()->toArray());
        $hhusertime = count(model('user')->field('id')->where([
            ['hhren','=','1'],
            ['create_time','>=','$today']
        ])->select()->toArray());
        $alluser =[
            'alluser'=>$alluser,
            'ptuser'=>$ptuser,
            'pduser'=>$pduser,
            'cpquser'=>$cpquser,
            'cpsuser'=>$cpsuser,
            'cpshuser'=>$cpshuser,
            'hhuser'=>$hhuser,
            'vipuser'=>$vipuser
        ];
        $allusertime =[
            'alluser'=>$allusertime,
            'ptuser'=>$ptusertime,
            'pduser'=>$pdusertime,
            'cpquser'=>$cpqusertime,
            'cpsuser'=>$cpsusertime,
            'cpshuser'=>$cpshusertime,
            'hhuser'=>$hhusertime,
            'vipuser'=>$vipusertime
        ];
        $this->assign('alluser',$alluser);
        $this->assign('allusertime',$allusertime);



        //商品模块
        //已上架商品总数
        $allproduct = count(model('product')->field('id')->select()->toArray());
        $allproducttime = count(model('product')->where('create_time','>=',$today)->field('id')->select()->toArray());
        //商品出售总数
        $sellproduct = model('product')->field('sell')->select()->toArray();
        $num = 0;
        foreach ($sellproduct as $v){
            $num = $num + $v['sell'];
        }
        $sellproducttime = model('product')->where('create_time','>=',$today)->field('sell')->select()->toArray();
        $numtime = 0;
        foreach ($sellproducttime as $v){
            $numtime = $numtime + $v['sell'];
        }
        //商品库存总数
        $stockproduct = model('product')->field('stock')->select()->toArray();
        $stock = 0;
        foreach ($stockproduct as $v){
            $stock = $stock + $v['stock'];
        }
        $stockproducttime = model('product')->where('create_time','>=',$today)->field('stock')->select()->toArray();
        $stocktime = 0;
        foreach ($stockproduct as $v){
            $stocktime = $stocktime + $v['stock'];
        }

        $allproduct = [
            'allproduct'=>$allproduct,
            'sellproduct'=>$num,
            'stockproduct'=>$stock,
        ];
        $allproducttime = [
            'allproduct'=>$allproducttime,
            'sellproduct'=>$numtime,
            'stockproduct'=>$stocktime,
        ];
        $this->assign('allproduct',$allproduct);
        $this->assign('allproducttime',$allproducttime);


        //订单模块
        //商品订单总数
        $orderproduct = count(model('order')->field('id')->select()->toArray());
        $orderproducttime = count(model('order')->field('id')->where('create_time','>=',$today)->select()->toArray());
        //已付款订单总数
        //'10'待付款, '20'待发货, '30'待收货, '40'已完成, '50'已取消
        $orderyf = count(model('order')->field('id')
            ->where('status','in','20,30,40')->select()->toArray());
        $orderyftime = count(model('order')->field('id')
            ->where([
                ['status','in','20,30,40'],
                ['create_time','>=',$today]
            ])->select()->toArray());
        //已完成订单总数
        $orderyw = count(model('order')->field('id')
            ->where('status','=','40')->select()->toArray());
        $orderywtime = count(model('order')->field('id')
            ->where([
                    ['status','=','40'],
                    ['create_time','>=',$today]
                ])->select()->toArray());


        $allorder = [
            'orderproduct'=>$orderproduct,
            'orderyf'=>$orderyf,
            'orderyw'=>$orderyw,
        ];

        $allordertime = [
            'orderproduct'=>$orderproducttime,
            'orderyf'=>$orderyftime,
            'orderyw'=>$orderywtime,
        ];

        $this->assign('allorder',$allorder);
        $this->assign('allordertime',$allordertime);


        //待处理模块
        //待发货订单
        $orderdf = count(model('order')->field('id')->where('status','=','20')->select()->toArray());
        $orderdftime = count(model('order')->field('id')->where([
            ['status','=','20'],
            ['create_time','>=',$today]
        ])->select()->toArray());
        //待处理注册
        //'10'通过, '20'待许可, '30'禁用
        $orderdc = count(model('user')->field('id')->where('status','=','20')->select()->toArray());
        $orderdctime = count(model('order')->field('id')->where([
            ['status','=','20'],
            ['create_time','>=',$today]
        ])->select()->toArray());

        $orderdf = [
            'orderdf'=>$orderdf,
            'orderdc'=>$orderdc
        ];

        $orderdftime = [
            'orderdf'=>$orderdftime,
            'orderdc'=>$orderdctime,
        ];

        $this->assign('orderdf',$orderdf);
        $this->assign('orderdftime',$orderdftime);
        return $this->fetch();
    }
}
